package com.zqj.project.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.dto.CartDTO;
import com.zqj.project.entity.CartPO;
import com.zqj.project.model.Cart;
import com.zqj.project.vo.CartVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: CartConverter
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 20:53
 * @Version: 1.0
 */
public class CartConverter {

    private CartConverter() {
        throw new IllegalStateException("Utility Class");
    }

    public static CartVO toCartVo(Cart cart) {
        return BeanUtil.toBean(cart, CartVO.class);
    }

    public static Cart toCartModel(CartDTO cartDto) {
        return BeanUtil.toBean(cartDto, Cart.class);
    }

    public static Cart toCartModel(CartPO cartPo) {
        return BeanUtil.toBean(cartPo, Cart.class);
    }

    public static CartPO toCartPo(Cart cart) {
        return BeanUtil.toBean(cart, CartPO.class);
    }

    public static List<CartVO> toCartVoList(List<Cart> cartModelList) {
        if (CollUtil.isEmpty(cartModelList)) {
            return Collections.emptyList();
        }
        return cartModelList.stream().map(e -> BeanUtil.toBean(e, CartVO.class)).toList();
    }

    public static List<Cart> toCartModelList(List<CartPO> cartPoList) {
        if (CollUtil.isEmpty(cartPoList)) {
            return Collections.emptyList();
        }
        return cartPoList.stream().map(e -> BeanUtil.toBean(e, Cart.class)).toList();
    }

    public static IPage<CartVO> toCartVoPage(IPage<Cart> cartModelPage) {
        IPage<CartVO> cartVoPage = new Page<>(cartModelPage.getCurrent(), cartModelPage.getSize());
        List<CartVO> cartVoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(cartModelPage.getRecords())) {
            cartModelPage.getRecords().forEach(entity -> cartVoList.add(BeanUtil.toBean(entity, CartVO.class)));
            cartVoPage.setRecords(cartVoList);
            cartVoPage.setTotal(cartModelPage.getTotal());
        }
        return cartVoPage;
    }

}